﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComLib.ConfigService;
using ComLib.ComType;
using LogLib;

namespace ProcessConfig.ConfigType
{
    /// <summary>
    /// 照片应用选项
    /// </summary>
    public class PhotoApplyOption : IniConfigBase
    {
        private ApplyTo _appType = ApplyTo.ExitAndEntry;
        public ApplyTo ApplyType
        {
            get { return _appType; }
            set { _appType = value; }
        }

        /// <summary>
        /// 标准文件路径
        /// </summary>
        private string _pxlFltPath = "epassport_ys";
        public string PxlFltPath
        {
            get { return _pxlFltPath; }
            set { _pxlFltPath = value; }
        }

        /// <summary>
        /// 源图像文件的保存格式
        /// </summary>
        private ImageFormats _sourceFormat = ImageFormats.Png;
        public ImageFormats SourceImageFormat
        {
            get { return _sourceFormat; }
            set { _sourceFormat = value; }
        }

        /// <summary>
        /// 输出图像的保存格式
        /// </summary>
        private ImageFormats _destImageFormat = ImageFormats.Jpeg;
        public ImageFormats DestImageFormat
        {
            get { return _destImageFormat; }
            set { _destImageFormat = value; }
        }
        public PhotoApplyOption()
        {

        }

        public PhotoApplyOption(ApplyTo apply)
        {
            _appType = apply;
        }

        public PhotoApplyOption(PhotoApplyOption clone)
        {
            _appType = clone.ApplyType;
            _sourceFormat = clone.SourceImageFormat;
            _destImageFormat = clone.DestImageFormat;
        }

        public override bool Save()
        {
            base.Save();
            SaveField("ApplyType", _appType);
            SaveField("PxlFltPath", _pxlFltPath);
            SaveField("SourceImageFormat", _sourceFormat);
            SaveField("DestImageFormat", _destImageFormat);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                _appType = LoadEnumValue<ApplyTo>("ApplyType");
                _pxlFltPath = LoadStringValue("PxlFltPath");
                _sourceFormat = LoadEnumValue<ImageFormats>("SourceImageFormat");
                _destImageFormat = LoadEnumValue<ImageFormats>("DestImageFormat");
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("照片应用选项::");         
            Action<string, object> FillFieldString =
             (string field, object value) =>
             {
                 builder.Append("[");
                 builder.Append(field);
                 builder.Append("]: ");
                 builder.Append(value.ToString());
                 builder.Append("; ");
             };
            FillFieldString("照片应用于的证件类型", _appType);
            FillFieldString("标准文件路径", _pxlFltPath);
            FillFieldString("源文件保存格式", _sourceFormat);
            FillFieldString("目标文件保存格式", _destImageFormat);
            return builder.ToString();
        }
    }
}
